home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / COMMAND.C < prev    next >
C/C++ Source or Header  |  1991-02-18  |  17KB  |  696 lines

  1. #include "pt.h"
  2. #include "malloc.h"
  3. #include "string.h"
  4. #include "process.h"
  5. #include "io.h"
  6. #include "stdlib.h"
  7. #include "conio.h"
  8.  
  9. #define STACKSIZE 2048
  10.  
  11. int pascal
  12. /* XTAG:command */
  13. command(fn, c, w)
  14.     int fn;
  15.     unsigned char c;
  16.     register struct window *w;
  17. {
  18.     extern unsigned char msgBuffer[];
  19.     extern unsigned char textBuffer[];
  20.     extern int debug;
  21.     extern struct window *selWindow;
  22.     extern struct window *activeWindow;
  23.     extern long selBegin, selEnd;
  24.     extern union REGS rin, rout;
  25.     extern int overType;
  26.     extern int searchMode;
  27.     extern int realSearchMode;
  28.     extern int mousePresent;
  29.     extern int scrRows, scrCols;
  30.     extern int i43lines;
  31.     extern unsigned char buttonVector[];
  32.     extern int menuRow, menuCol;
  33.     extern int lastOnTopline;
  34.     extern int lastCommand;
  35.     extern int macroIndex;
  36.     extern int macroSize;
  37.     extern int macroState;
  38.     extern int selMode;
  39.     extern int lastFn;
  40. /*    extern int errno;  */
  41. #ifdef OVERLAYS
  42.     extern int filesToOpen;
  43.     extern int primaryFilesToOpen;
  44.     extern struct openFile *ffsToOpen[];
  45.     extern unsigned char *overlayMap[];
  46.     extern unsigned char *stktop;
  47.     extern unsigned char primaryOverlay[];
  48.     extern unsigned char secondaryOverlay[];
  49.     extern unsigned char overlayString[];
  50.     extern unsigned char *overlayArguments;
  51.     extern int overlayState;
  52.     extern int quitExecuted;
  53. #endif
  54.     extern unsigned char *userMessages[];
  55.     extern struct openFile *files;
  56.     extern unsigned char maxTextCycles;
  57.     extern unsigned char maxBorderCycles;
  58.     extern struct SREGS segRegs;
  59.     extern struct menuBlock far *menus[];
  60.     extern struct optionItem options[];
  61.     extern unsigned char saveVideoMode;
  62.     extern union REGS rin, rout;
  63.     extern unsigned int dispMemory;
  64.  
  65.     int row1, col1;
  66.     int r1, r2;
  67.     int i, n;
  68.     long cp;
  69.     unsigned char ch;
  70.     unsigned char *fileName;
  71.     struct window *w2;
  72.     unsigned char *s, buffer[64];
  73. #ifdef OVERLAYS
  74.     unsigned char *userArgs;
  75.     unsigned char *saveStktop;
  76. #endif
  77.  
  78. /* since we are about to issue a command, we do not need to erase */
  79. /* the command description on the top line (so don't) */
  80. lastOnTopline = 0;
  81.  
  82. startOver:
  83.  
  84. #ifdef OVERLAYS
  85. if( 200 <= fn && fn <= 255 ) {
  86.     fileName = overlayMap[fn-200];
  87. doOverlay:
  88.  
  89.     n = strlen(fileName) - 1;
  90.     if( fileName[n] == ' ' )
  91.         userArgs = getInput("Execute: ", fileName, 5);
  92.     else
  93.         userArgs = fileName;
  94.  
  95.     /* allow user to cancel */
  96.     if( userArgs == NULL ) {
  97.         msg("Command cancelled", 1);
  98.         goto returnLocation;
  99.     }
  100.  
  101.     /* make a copy of the command line */
  102.     strncpy(overlayString, userArgs, 128);
  103.  
  104.     overlayArguments = strchr(overlayString, ' ');
  105.     if( overlayArguments != NULL )
  106.         *overlayArguments++ = '\0';
  107.     else
  108.         overlayArguments = "";
  109.  
  110.     /* see if this overlay is loaded and inactive */
  111.  
  112.     /* see if this is a primary or secondary overlay */
  113.     n = strlen(overlayString) - 1;
  114.     if( overlayString[n] == '*' ) {    /* primary overlay is to be loaded */
  115.         /* eliminate the '*' which means the overlay is primary */
  116.         overlayString[n] = '\0';
  117.  
  118.         if( (overlayState & 0x1) != 0 ) {
  119.             /* a primary overlay is already loaded */
  120.             if( strcmp(primaryOverlay, overlayString) == 0 ) {
  121.                 /* same overlay, reactivate this one */
  122.                 if( (overlayState & 0x2) != 0 ) {
  123.                     strcpy(textBuffer, "0");
  124.                     secondaryOverlay[0] = '\0';
  125.                     overlayState &= ~0x2;
  126.                     overlayState |= 0x14;
  127.                 } else {
  128.                     strcpy(textBuffer, overlayArguments);
  129.                 }
  130.             } else {
  131.                 /* remember the name of the new overlay */
  132.                 strcpy(primaryOverlay, overlayString);
  133.                 strcpy(textBuffer, "0");
  134.                 /* indicate we are unloading a primary */
  135.                 /* overlay */
  136.                 overlayState |= 0x8;
  137.                 /* check for secondary overlay */
  138.                 if( (overlayState & 0x2) != 0 ) {
  139.                     overlayState &= ~0x2;
  140.                     overlayState |= 0x4;
  141.                     secondaryOverlay[0] = '\0';
  142.                 } else {
  143.                 }
  144.             }
  145.             /* Drop out of command loop. This will return to */
  146.             /* "overlayDriver()" which will return (from the */
  147.             /* software interrupt)to the overlay. */
  148.             return 1;
  149.         } else {
  150.             strcpy(primaryOverlay, overlayString);
  151.             overlayState |= 0x1;
  152.             /* see if we have to unload the secondary overlay */
  153.             if( (overlayState & 0x2) != 0 ) {
  154.                 strcpy(textBuffer, "0");
  155.                 overlayState &= ~0x2;
  156.                 overlayState |= 0x4;
  157.                 secondaryOverlay[0] = '\0';
  158.                 return 1;
  159.             }
  160.             /* else, drop though and load the overlay */
  161.         }
  162.     } else {    /* secondary overlay is to be loaded */
  163.         if( (overlayState & 0x2) != 0 ) {
  164.             /* a secondary overlay is already loaded */
  165.             if( strcmp(secondaryOverlay, overlayString) == 0 ) {
  166.                 /* same overlay, reactivate this one */
  167.                 strcpy(textBuffer, overlayArguments);
  168.             } else {
  169.                 /* different overlay, unload this one */
  170.                 /* 0 argument means to exit the overlay */
  171.                 strcpy(textBuffer, "0");
  172.                 strcpy(secondaryOverlay, overlayString);
  173.                 /* indicate we are unloading an overlay */
  174.                 overlayState |= 0x4;
  175.             }
  176.             /* Drop out of command loop. This will return to */
  177.             /* "overlayDriver()" which will return (from the */
  178.             /* software interrupt)to the overlay. */
  179.             return 1;
  180.         } else {
  181.             strcpy(secondaryOverlay, overlayString);
  182.             overlayState |= 0x2;
  183.             /* drop though and load the overlay */
  184.         }
  185.     }
  186.  
  187. retryOverlay:
  188.     /* save the old value of stktop */
  189.     saveStktop = stktop;
  190.  
  191.     /* set up the stack to use when the overlay calls us back */
  192.     stktop = malloc(STACKSIZE);
  193.     if( stktop == NULL ) {
  194.         msg(userMessages[NOSPACEMSG], 3);
  195.         goto returnLocation;
  196.     }
  197.     stktop += STACKSIZE-8;
  198.  
  199.     /* We have to reopen any files opened by the overlay when it */
  200.     /* exits since DOS will close them automatically. If fileToOpen */
  201.     /* is already non-zero then an overlay is already loaded. */
  202.     if( filesToOpen == 0 )
  203.         filesToOpen = 1;
  204.     else
  205.         primaryFilesToOpen = filesToOpen;
  206.  
  207.     n = spawnlp(0, overlayString, overlayString, overlayArguments, NULL);
  208.     if( n == -1 ) {
  209.         sprintf(msgBuffer,
  210. "Overlay %s could NOT be executed [Press any key to continue]",
  211.             overlayString);
  212.         msg(msgBuffer, 3);
  213.     } else if( n > 0 ) {
  214.         sprintf(msgBuffer, "Overlay %s returned error code of 0x%X",
  215.             overlayString, n);
  216.         msg(msgBuffer, 1);
  217.     } 
  218.  
  219.     /* free up the stack space */
  220.     stktop -= STACKSIZE-8;
  221.     free(stktop);
  222.     /* now restore the previous value of stktop */
  223.     stktop = saveStktop;
  224.  
  225.     /* reopen any files that were opened during the overlay */
  226.     /* since DOS will have closed them automatically */
  227.     /* if this is a secondary overlay that is exiting, then only */
  228.     /* reopen the files that were opened since the secondary */
  229.     /* overlay was loaded */
  230.     if( primaryFilesToOpen > 0 )
  231.         n = primaryFilesToOpen;
  232.     else
  233.         n = 1;
  234.     for(i = n; i < filesToOpen; ++i) {
  235.         ff = ffsToOpen[i-1];
  236.         ff->origHandle = openls(ff->origName, 0);
  237.     }
  238.     if( primaryFilesToOpen > 0 ) {
  239.         filesToOpen = primaryFilesToOpen;
  240.         primaryFilesToOpen = 0;
  241.     } else
  242.         filesToOpen = 0;
  243.     
  244.     if( quitExecuted )
  245.         return 1;
  246.  
  247.     if( (overlayState & 0x4) != 0 ) {
  248.         /* see if we have to unload the primary overlay also */
  249.         overlayState &= ~0x4;
  250.         if( (overlayState & 0x8) != 0 ) {
  251.             strcpy(textBuffer, "0");  /* unload overlay arg. */
  252.             return 1;
  253.         }
  254.         if( (overlayState & 0x10) != 0 ) {
  255.             overlayState &= ~0x10;
  256.             strcpy(textBuffer, overlayArguments);
  257.             return 1;
  258.         }
  259.         goto retryOverlay;
  260.     } else if( (overlayState & 0x8) != 0 ) {
  261.         overlayState &= ~0x8;
  262.         goto retryOverlay;
  263.     } else if( (overlayState & 0x2) != 0 ) {
  264.         overlayState &= ~0x2;
  265.         secondaryOverlay[0] = '\0';
  266.     } else {    /* overlayState == 0x1 (primary overlay only) */
  267.         primaryOverlay[0] = '\0';
  268.         overlayState = 0;
  269.     }
  270.  
  271.     initMouse(5, 10, 0);
  272.     goto returnLocation;
  273. }
  274. #endif
  275.  
  276. if( fn < 50 ) {
  277.     n = command2(fn, c, w);
  278.     if( fn != FMOUSECOM1 && fn != FMOUSECOM2
  279.         && fn != FDONOTHING && fn > 0 ) {
  280.         lastCommand = fn;
  281.     }
  282.     lastFn = fn;
  283.     if( n != 2 )
  284.         return n;
  285.     /* return value of 2 means that select and extend want the */
  286.     /* change menu command to be executed */
  287.     fn = FCHANGEMENU;
  288.     /* and drop through since change menu >= 50 */
  289. }
  290.  
  291. /* else do commands 50 and up */
  292.  
  293. switch( fn ) {
  294.  
  295. case FAUTOSAVEINTERVAL:case FAUTOSCROLLRATE:case FAUTOINDENT:
  296. case FBORDERCOLORS:case FCENTERMENUS:
  297. ca